<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Zizaco\Entrust\Traits\EntrustUserTrait;

class User extends Authenticatable
{
    use Notifiable, EntrustUserTrait, HasApiTokens;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'avatar'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function checkUserRoleFlag(User $target)
    {
        $roles = $this->roles;
        $targetRoles = $target->roles;
        foreach ($roles as $role) {
            foreach ($targetRoles as $targetRole) {
                if ($targetRole->flag >= $role->flag) {
                    return false;
                }
            }
        }
        return true;
    }
}
